package com.happypets.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.happypets.model.UserBean;
import com.happypets.servicesImpl.UserServiceImpl;
import com.happypets.utils.UploadUtil;

/**
 * Servlet implementation class UpdateUserInfoServlet
 */
public class UpdateUserInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateUserInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		UserServiceImpl userSer = new UserServiceImpl();
		
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		Gson gson=new Gson();
		Map<String,String> map = new HashMap<String,String>();
		String flag = request.getParameter("flag");

		
		
		if(flag.equals("1")){
			//flag = 1 更新大  	2更新照片
			String id = request.getParameter("id");
			String name = request.getParameter("name");
			String passwd = request.getParameter("passwd");
			String sex = request.getParameter("sex");
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			String provience = request.getParameter("provience");
			String city = request.getParameter("city");
			String country = request.getParameter("country");
			String address = request.getParameter("address");
			String realName = request.getParameter("realName");
			String bir = request.getParameter("bir");
			
			UserBean user = new UserBean();
			
			user.setUserId(id);
			user.setPassword(passwd);
			user.setUserAddress(address);
			user.setUserAddPro(provience);
			user.setUserAddCity(city);
			user.setUserAddCou(country);
			user.setUserEmail(email);
			user.setUserName(name);
			user.setUserPhone(phone);
			user.setUserSex(sex);
			user.setUserRealName(realName);
			user.setUserBir(bir);
			if(userSer.updateUser(user) == 1){
				map.put("flag","1");
				map.put("msg","修改成功");
				session.removeAttribute("user");
				user = userSer.getUserBeanByName(name);
				session.setAttribute("user", user);
			}else{
				map.put("flag","2");
				map.put("msg","修改失败");
			}
		}else{
			
			Map<String,String> pm =  UploadUtil.fileUpload(request);
			
			String fileName = pm.get("photo");
			String id = pm.get("id");
			System.out.println("fileName2:"+fileName);
			
			UserBean user = new UserBean();
			
			user.setUserId(id);
			user.setUserPhoto(fileName);
			if(userSer.updateUserPhoto(user) == 1){
				map.put("flag","1");
				map.put("msg","修改成功");
				session.removeAttribute("user");
				user = userSer.getUserBeanById(id);
				session.setAttribute("user", user);
				response.sendRedirect(request.getContextPath()+"/web/userInfo.jsp");
			}else{
				map.put("flag","2");
				map.put("msg","修改失败");
			}
			
		}
		List<Map<String,String>> lm = new ArrayList<Map<String,String>>();
		lm.add(map);
		String json=gson.toJson(lm);
		System.out.println(json);
		out.write(json);
		
		out.flush();
		out.close();
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
